package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.constant.ExcelConstants;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.utils.GoFastDfsUtil;
import com.gzhu.knowledgeAdmin.common.valid.AddGroup;
import com.gzhu.knowledgeAdmin.common.valid.UpdateGroup;
import com.gzhu.knowledgeAdmin.common.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.gzhu.knowledgeAdmin.entity.Zs;
import com.gzhu.knowledgeAdmin.service.ZsService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

/**
 * 知识表
 *
 * @author giaogiao
 * @email giaogiao@gmail.com
 * @date 2023-04-22 23:18:59
 */
@RestController
@RequestMapping("knowledgeAdmin/zs")
@CrossOrigin
public class ZsController {
    @Autowired
    private ZsService zsService;

    @Autowired
    private GoFastDfsUtil goFastDfsUtil;
    // 测试
    /**
     * 在指定公司的知识库里，根据问题找到回答
     * @param queryVo
     * @return
     */
    @PostMapping("getAnswer")
    public R getAnswer(@RequestBody KnowledgeQueryVo queryVo){

       List<String> answers =  zsService.getAnswerByQuestion(queryVo);
       return R.ok(answers);
    }

    /**
     * 根据公司id缓存知识库
     * @param gsId
     * @return
     */
    @GetMapping("/cache/{id}")
    public R cacheZs(@PathVariable("id")String gsId){
        zsService.cacheZsByGsId(gsId);
        return R.ok();
    }

    /**
     * 获取知识详情信息
     * @return
     */
    @GetMapping("/detail/{id}")
    @PreAuthorize("hasAuthority('knowledge.detail')")
    public R detail(@PathVariable("id")Integer id){
        KnowledgeDetailVo detail = zsService.getZsDetail(id);
        return R.ok(detail);
    }

    /**
     * 获取所有一级知识分类（即问题类型）
     *
     * @return
     */
    @GetMapping("/oneLevel")
    public R getAllOneLevel() {
        List<OneLevel> list = zsService.getAllOneLevel();
        return R.ok(list);
    }

    /**
     * 知识生效
     *
     * @return
     */
    @PutMapping("/effective")
    @PreAuthorize("hasAuthority('knowledge.audit')")
    public R effective(@RequestBody List<Integer> ids) {
        zsService.effective(ids);
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('knowledge.list')")
    public R list(@RequestParam Map<String, Object> params) {
        PageVo page = zsService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('knowledge.update')")
    public R info(@PathVariable("id") Integer id) {
        KnowledgeResponseVo zs = zsService.getZsById(id);
        return R.ok(zs);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('knowledge.add')")
    public R save(@RequestBody @Validated(AddGroup.class) KnowledgeAddOrUpdateVo zs) {
        zsService.saveZs(zs);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('knowledge.update')")
    public R update(@RequestBody @Validated(UpdateGroup.class) KnowledgeAddOrUpdateVo knowledgeAddOrUpdateVo) {
        zsService.updateZs(knowledgeAddOrUpdateVo);
        return R.ok();
    }


    /**
     * 下线知识
     */
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('knowledge.delete')")
    public R delete(@RequestBody List<Integer> ids) {
        zsService.removeZsByIds(ids);
        return R.ok();
    }

    /**
     * 知识审核不通过
     * @return
     */
    @PostMapping("/notPass")
    public R notPass(@RequestBody NotPassVo notPassVo){
        zsService.notPass(notPassVo);
        return R.ok();
    }

    /**
     * 获取知识审核不通过详情
     * @param id 知识id
     * @return
     */
    @GetMapping("/failDetail/{id}")
    public R FailDetail(@PathVariable("id")Integer id){
        KnowledgeFailDetailVo failDetail = zsService.getZsFailDetail(id);
        return R.ok(failDetail);
    }

    /**
     * 我的知识列表
     */
    @GetMapping("/myKnowledgeList")
    @PreAuthorize("hasAuthority('knowledge.list')")
    public R myKnowledgeList(@RequestParam Map<String, Object> params) {
        PageVo page = zsService.queryMyKnowledgePage(params);
        return R.ok(page);
    }

    /**
     * 修改后重新发起审核
     */
    @PutMapping("/updateAndReEamine")
    @PreAuthorize("hasAuthority('knowledge.update')")
    public R updateAndReEamine(@RequestBody @Validated(UpdateGroup.class) KnowledgeAddOrUpdateVo knowledgeAddOrUpdateVo) {
        zsService.updateAndReEamine(knowledgeAddOrUpdateVo);
        return R.ok();
    }

    @GetMapping("/search")
    public R test(@RequestParam Map<String, Object> params){
        PageVo res = zsService.search(params);
        return R.ok(res);
    }

    /**
     * 知识导入模板下载
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) throws IOException, URISyntaxException {
        goFastDfsUtil.downloadFile(goFastDfsUtil.getServicePath()+'/'+ ExcelConstants.ZS_TEMPLATE_URL,response);
    }


    /**
     * 导入知识
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/_import")
    public R importKeyword(MultipartFile file) throws IOException {
        String msg = zsService.importZS(file);
        return R.ok(msg);
    }


}
